package su.myspringapps.onlineschool.repository;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import su.myspringapps.onlineschool.entity.Course;
import su.myspringapps.onlineschool.entity.Student;
import su.myspringapps.onlineschool.entity.Subscription;

import java.util.List;

@SpringBootTest
class SubscriptionRepositoryTest {

    @Autowired
    SubscriptionRepository subscriptionRepository;

    @Autowired
    CourseRepository courseRepository;

    @Autowired
    StudentRepository studentRepository;

    @Test
    void subscribeStudentToCourse() throws Exception {
        testSubscribeAndUnsubscribe();
    }

    @Test
    void unsubscribeStudentFromCourse() throws Exception {
        testSubscribeAndUnsubscribe();
    }

    @Test
    void testFindAll() throws Exception {
        List<Subscription> subscriptionList = subscriptionRepository.findAll();
        Subscription actualSubscription1 = subscriptionList.get(0);
        Subscription actualSubscription2 = subscriptionList.get(1);
        Subscription actualSubscription3 = subscriptionList.get(2);
        Assertions.assertEquals(1, actualSubscription1.getStudent().getId());
        Assertions.assertEquals(2, actualSubscription1.getCourse().getId());
        Assertions.assertEquals("2018-01-01 00:00:00.0", actualSubscription1.getSubscriptionDate().toString());
        Assertions.assertEquals(1, actualSubscription2.getStudent().getId());
        Assertions.assertEquals(10, actualSubscription2.getCourse().getId());
        Assertions.assertEquals("2018-04-11 00:00:00.0", actualSubscription2.getSubscriptionDate().toString());
        Assertions.assertEquals(2, actualSubscription3.getStudent().getId());
        Assertions.assertEquals(1, actualSubscription3.getCourse().getId());
        Assertions.assertEquals("2018-01-02 00:00:00.0", actualSubscription3.getSubscriptionDate().toString());
    }

    private void testSubscribeAndUnsubscribe() throws Exception {
        Student expectedStudent = studentRepository.findById(1);
        Course expectedCourse = courseRepository.findById(1);
        subscriptionRepository.subscribeStudentToCourse(expectedStudent, expectedCourse);
        Subscription actualSubscription = subscriptionRepository.findAll().get(271);
        Student actualStudent = actualSubscription.getStudent();
        Course actualCourse = actualSubscription.getCourse();
        Assertions.assertEquals(expectedStudent.getId(), actualCourse.getId());
        Assertions.assertEquals(expectedStudent.getName(), actualStudent.getName());
        Assertions.assertEquals(expectedCourse.getId(), actualCourse.getId());
        Assertions.assertEquals(expectedCourse.getName(), actualCourse.getName());
        subscriptionRepository.unsubscribeStudentFromCourse(actualStudent, actualCourse);
        Assertions.assertEquals(271, subscriptionRepository.findAll().size());
    }
}